<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Book_Booklet extends Controller{

public function action_index()
{
}

public function action_list()
{
	$booklet = ORM::factory('booklet');
	if ($_GET)
	{
		$category_id = arr::get($_GET, 'category_id', -1);
		if ($category_id != -1)
		{
			$booklet->where('category_id', '=', $category_id);
		}
	}
	$query_result = $booklet->find_all();
	$result = $this->create_booklet_list($query_result->as_array());
	echo json_encode($result);
}

public function action_comment()
{
	$booklet_comment = ORM::factory('comment_booklet');
	if ($_GET)
	{
		$booklet_id = arr::get($_GET, 'booklet_id', -1);
		if ($booklet_id != -1)
		{
			$booklet_comment->where('booklet_id', '=', $booklet_id);
		}
	}
	$query_result = $booklet_comment->find_all();
	$result = $this->create_booklet_comment_list($query_result->as_array());
	echo json_encode($result);
}

protected function create_booklet_list($array)
{
	$object = array();
	foreach ( $array as $booklet)
	{
		array_push($object,
			array(
				'id'			=> $booklet->id,
				'name'			=> $booklet->name,
				'description'	=> $booklet->description,
				'image'   		=> $booklet->image,
			));
	}
	return array(
		'count' => count($object),
		'data'  => $object
	);
}

protected function create_booklet_comment_list($array)
{
	$object = array();
	foreach ( $array as $booklet_comment)
	{
		array_push($object,
			array(
				'id'		   => $booklet_comment->id,
				'content'	   => $booklet_comment->content,
				'ip'   		   => $booklet_comment->ip,
				'create_time'  => $booklet_comment->create_time,
			));
	}
	return array(
		'count' => count($object),
		'data'  => $object
	);
}


}



